package com.jack.tang.netty.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class BioSocket {


    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(8089);
        socketDemo1( serverSocket);
    }

    public static void socketDemo1(ServerSocket serverSocket) throws IOException {

        while (true) {
            Socket accept = serverSocket.accept();
            handleSocket( accept);
        }
    }
    public static void socketDemo2(ServerSocket serverSocket) throws IOException {

        while (true) {
            Socket accept = serverSocket.accept();
            new Thread(() -> {
                handleSocket( accept);
            }).start();
        }
    }





    //生成一个处理socket数据的方法
    public static void handleSocket(Socket socket) {
        try {
            byte[] bytes = new byte[1024];
            //处理socket数据
            int read = socket.getInputStream().read(bytes);
            System.out.println(read);
            if (read != -1) {
                System.out.println(new String(bytes, 0, read));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
